package com.aaa.controller.user;

import com.aaa.entity.ResponseDto;
import com.aaa.entity.User;
import com.aaa.service.UserService;
import com.aaa.service.impl.UserServiceImpl;
import com.aaa.util.IntegerUtils;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 会员列表修改    使用CardId进行修改
 */
@WebServlet("/UpdateUserByCardIdServlet")
public class UpdateUserByCardIdServlet extends HttpServlet {

    private UserService userService;

    @Override
    public void init() throws ServletException {
        userService = new UserServiceImpl();
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        ResponseDto responseDto = new ResponseDto();
        try {
            String userName = req.getParameter("userName");
            String phone = req.getParameter("phone");
            Integer status = IntegerUtils.ToInteger(req.getParameter("status"));
            String idCard = req.getParameter("idCard");
            String address = req.getParameter("address");
            Integer cardId = IntegerUtils.ToInteger(req.getParameter("cardId"));

            User user = new User();
            user.setUserName(userName);
            user.setPhone(phone);
            user.setIdCard(idCard);
            user.setStatus(status);
            user.setAddress(address);
            user.setCardId(cardId);
            System.out.println(user);
            userService.updateUserByUserId(user);
            responseDto.setStatus(ResponseDto.SUCCESS_CODE);
            responseDto.setMessage("请求成功");
        }catch (Exception e){
            e.printStackTrace();
            responseDto.setStatus(ResponseDto.FAILURE_CODE);
            responseDto.setMessage("请求失败");
        }
        resp.getWriter().write(new Gson().toJson(responseDto));
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

}
